package al.xc.service.impl;

import al.xc.common.util.GsonUtils;
import al.xc.common.util.SingleUtils;
import al.xc.common.util.TokenUtils;
import al.xc.data.dto.SessionInfo;
import al.xc.redis.RedisConnectManager;
import al.xc.service.ITokenService;
import org.springframework.stereotype.Service;

@Service
public class TokenServiceImpl implements ITokenService {

    private RedisConnectManager redisManager = SingleUtils.instance(RedisConnectManager.class);

    /**
     * 查询token信息
     *
     * @param token
     * @return
     */
    public SessionInfo find(String token) {
        Integer[] vls = TokenUtils.decryptToken(token);
        if (null == vls || vls.length != 2) {
            return null;
        }

        String token_info = redisManager.connect().hget("CLIENT_TOKEN", vls[0] + "|" + vls[1]);
        if (null == token_info || token_info.equals("")) {
            return null;
        }

        SessionInfo result = null;
        try {
            result = GsonUtils.fromJson(token_info, SessionInfo.class);
        } catch (Exception ex) {
            return null;
        }
        return result;
    }
}
